<%--
  Created by IntelliJ IDEA.
  User: 86181
  Date: 2022/11/17
  Time: 10:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登记模态框</title>
</head>
<body>
        <form id="outboundregistrationmodalform">
            <div class="modal-header">
                <input type="submit" class="btn btn-default" value="提交">
                <button class="close" data-dismiss="modal">退出</button>
                <input type="hidden" name="id" id="outboundregistraptionmodalid">
            </div>
            <div class="modal-body">
                    <div class="row">
                        <div class="col-md-offset-5"><h2><strong>出库单</strong></h2></div>
                    </div>
                    <br>
                    <div class="row">
                        <div class="col-md-6">
                            <table>
                                <tr>
                                    <td>出库单编号:</td>
                                    <td><input type="text" name="payId" id="outboundregistrationmodalpayId" readonly style="outline: none;border: none;"></td>
                                </tr>
                                <tr>
                                    <td>出库理由:</td>
                                    <td><input type="text"  id="outboundregistrationmodalreason" readonly style="outline: none;border: none;"></td>
                                </tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <table>
                                <tr>
                                    <td>出库理由详情：</td>
                                    <td><input id="outboundregistrationmodalreasonexact" type="text" readonly style="outline: none;border: none"></td>
                                </tr>
                            </table>
                        </div>
                        <br>
                        <div style="height:45%;">
                            <table id="outboundregistrationmodaltab"></table>
                        </div>
                        <br>
                        <div class="row">
                            <div class="col-md-6">
                                <table>
                                    <tr>
                                        <td>应出库数量:</td>
                                        <td><input type="text" id="outboundregistrationmodalamountSum" readonly style="outline: none;border: none"></td>
                                    </tr>
                                    <tr>
                                        <td>应出库总成本:</td>
                                        <td><input type="text" id="outboundregistrationmodalcostPriceSum" readonly style="outline: none;border: none"></td>
                                    </tr>
                                    <tr>
                                        <td>登记人:</td>
                                        <td><input type="text" name="register" id="outboundregistrationmodalregister" required style="outline: none;border: none;border-bottom: 1px black dashed"></td>
                                    </tr>
                                </table>
                            </div>
                            <div class="col-md-6">
                                <table>
                                    <tr>
                                        <td>已出库数量:</td>
                                        <td><input type="text" name="paidAmountSum"  id="outboundregistrationmodalpaidAmountSum" readonly style="outline: none;border: none"></td>
                                    </tr>
                                    <tr>
                                        <td>已出库总成本:</td>
                                        <td><input type="text" id="outboundregistrationmodalcostPriceSums" readonly style="outline: none;border: none"></td>
                                    </tr>
                                    <tr>
                                        <td>登记时间:</td>
                                        <td><input type="text" name="registerTime" id="outboundregistrationmodalregisterTime" readonly style="outline: none;border: none;"></td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
            </div>
        </form>
            <script>
                //加载表格数据
                $(function () {
                    $("#outboundregistrationmodaltab").bootstrapTable({
                        sortable: true,//是否显示排序
                        sortOrder: "aso",//排序方式
                        sidePagination:"server", //分页方式：client客户端分页，server服务端分页（*）
                        showColumns: true,//是否显示所有的列（选择显示的列）
                        showRefresh: true,//是否显示刷新按钮
                        minimumCountColumns: 2,//最少允许的列数
                        showToggle: true,//是否显示详细视图和列表视图的切换按钮
                        cardView: false,//是否显示详细视图
                        detailView: false,//是否显示父子表,
                        queryParams:function (params) {
                            var outboundregistrationmodal={
                                pagesize:params.limit,
                                pageno:(params.offset/params.limit)+1,
                                sort:params.sort,
                                sortOrder:params.sort,
                            }
                            return outboundregistrationmodal
                        },
                        columns:[
                            {
                                field:"",
                                title:"序号",
                                formatter:function (value,row,index) {
                                    return Number(index)+1
                                }
                            },
                            {
                                field:"productId",
                                title:"产品编号",
                            },
                            {
                                field:"productName",
                                title:"产品名称",
                            },
                            {
                                field:"costPrice",
                                title:"成本价格",
                            },
                            {
                                field:"cell",
                                title:"当前库存",
                                formatter:function (value,row,index) {
                                    return value.amount
                                }
                            },
                            {
                                field:"amount",
                                title:"应出库数量",
                            },
                            {
                                field:"warehousing",
                                title:"已出库数量",
                            },
                           /* {
                                field:"paidAmount",
                                title:"出库数量",
                            },*/
                            {
                                field:"paidAmount",
                                title:"本次出库数量",
                                formatter:function (value,row,index) {
                                   if(Number(row.amount)!=Number(row.warehousing)){
                                       return "<input type='number' value='"+value+"' style='width:80px' required onchange='changenum(this)' class='amountnum'>"
                                   }else {
                                       return "完成"
                                   }
                                }
                            },

                        ]
                    })
                    //获取值
                    var payId='${param.payId}'
                    $.post("queryByPayCellPayDetails.action",{"payId":payId},function (data) {
                        console.log(data)
                        //出库编号
                        $("#outboundregistrationmodalpayId").val(data.payId)
                        //出库理由
                        var reasons="";
                        if(data.reason=="C002-1"){
                            reasons="生产领料"
                        }
                        if(data.reason=="C002-2"){
                            reasons="赠送"
                        }
                        if(data.reason=="C002-3"){
                            reasons="内部借领"
                        }
                        if(data.reason==" C002-4"){
                            reasons="其他借领"
                        }
                        $("#outboundregistrationmodalreason").val(reasons)
                        //出库详情理由
                        $("#outboundregistrationmodalreasonexact").val(data.reasonexact)
                        //应出库数量
                        $("#outboundregistrationmodalamountSum").val(data.amountSum)
                        //应出库总成本
                        $("#outboundregistrationmodalcostPriceSum").val(data.costPriceSum)
                        //登记人
                        $("#outboundregistrationmodalregister").val(data.register)
                        $("#outboundregistraptionmodalid").val(data.id)
                        //登记时间
                        //点击获取当前时间
                        $.get("queryCellByDate.action",function (datas) {
                            $("#outboundregistrationmodalregisterTime").val(datas.datetime)
                        },"json")
                        //加载表格数据集
                        var arr=[]
                        var arrs=data.payDetails;
                        for(var i=0;i<arrs.length;i++){
                            arr.push(arrs[i])
                        }
                        //加载给表格
                        $("#outboundregistrationmodaltab").bootstrapTable("append",arr)
                        //获取值
                        var paydetalis=$("#outboundregistrationmodaltab").bootstrapTable("getData")
                        var i=0;
                        console.log(data.payDetails)
                        var nums=0;
                        $(paydetalis).each(function (index,item) {
                            i+=Number(item.warehousing);
                            nums+=i*data.payDetails[index].costPrice;
                        })
                        //已出库数量
                        $("#outboundregistrationmodalpaidAmountSum").val(i)
                        //已出库总成本
                        $("#outboundregistrationmodalcostPriceSums").val(nums)
                    },"json")
                })
                //输入数字，将数字更新到表格中
                function changenum(obj){

                    //获取表格当前所有数据
                    var arr2 =$("#outboundregistrationmodaltab").bootstrapTable("getData");

                    //获取当前输入的值
                    var num = $(obj).val();

                    //获取当前操作行的id
                    var id = $(obj).parent().parent().find("td:eq(1)").text();

                    //循环表格数据  和当前行id比较  相等  跟新num
                    $(arr2).each(function (index,item) {
                        if(item.productId==id){
                            item.paidAmount = num;
                        }
                    })
                    //数据加载到表格
                    $("#outboundregistrationmodaltab").bootstrapTable("load",arr2);

                }
                //提交
            /*    $("#outboundregistrationmodalform").validate({

                    submitHandler:function () {
                        //获取值
                        var paydetalis=$("#outboundregistrationmodaltab").bootstrapTable("getData")
                        var f=false;
                        var reg=/^(([^0][0-9]+|0)\.([0-9]{1,2})$)|^([^0][0-9]+|0)$/
                        $(paydetalis).each(function (index,item) {
                            console.log(item.paidAmount)
                            //验证数字
                            if(reg.test(item.paidAmount)){

                                if(Number(item.paidAmount)+Number(item.warehousing)<=Number(amount)){
                                    f=true;
                                    return;
                                }else{
                                    alert("入库数量不正确")
                                }
                            }else{
                                alert("aaa")
                            }

                        })
                        if(f){
                            $(".amountnum").each(function (index,item) {
                                alert(item.value)
                                paydetalis[index].paidAmount=item.value;
                            })
                            $("#outboundregistrationmodaltab").bootstrapTable("load",paydetalis)

                            //获取from的值
                            var pay={};
                            var arr=$("#outboundregistrationmodalform").serializeArray();
                            $(arr).each(function (index,item) {
                                pay[item.name]=item.value;
                            })
                            //获取表格的值
                            var paydetails=$("#outboundregistrationmodaltab").bootstrapTable("getData")
                            pay["payDetails"]=paydetails
                            console.log(pay)

                            $.ajax({
                                url:"updatoutpeyPaydetailsBystatus.action",
                                data:JSON.stringify(pay),
                                contentType:"application/json",
                                type:"post",
                                dataType:"json",
                                success:function (data) {
                                    alert(data.msg)
                                    //关闭模态框刷新表格
                                    $("#outboundregistrationmodal").modal("hide")
                                    $("#outboundregistrationtab").bootstrapTable("refresh")
                                }
                            })
                        }


                    }

                })*/
                //提交
                $("#outboundregistrationmodalform").validate({
                    submitHandler:function () {
                        //获取值
                        var i=0;
                        var paydetalis=$("#outboundregistrationmodaltab").bootstrapTable("getData")
                        $(paydetalis).each(function (index,item) {
                            if(Number(item.paidAmount)+Number(item.warehousing)>Number(item.amount)){
                                i++;
                            }
                        })
                        if(i==0){
                            $(".amountnum").each(function (index,item) {
                                paydetalis[index].paidAmount=item.value;
                            })
                            $("#outboundregistrationmodaltab").bootstrapTable("load",paydetalis)

                            //获取from的值
                            var pay={};
                            var arr=$("#outboundregistrationmodalform").serializeArray();
                            $(arr).each(function (index,item) {
                                pay[item.name]=item.value;
                            })
                            //获取表格的值
                            var paydetails=$("#outboundregistrationmodaltab").bootstrapTable("getData")
                            pay["payDetails"]=paydetails
                            console.log(pay)

                            $.ajax({
                                url:"updatoutpeyPaydetailsBystatus.action",
                                data:JSON.stringify(pay),
                                contentType:"application/json",
                                type:"post",
                                dataType:"json",
                                success:function (data) {
                                    alert(data.msg)
                                    //关闭模态框刷新表格
                                    $("#outboundregistrationmodal").modal("hide")
                                    $("#outboundregistrationtab").bootstrapTable("refresh")
                                }
                            })

                        }else {
                            alert("入库数量不正确")
                        }

                    }

                })
            </script>
</body>
</html>
